#include "napi/native_api.h"
#include <hilog/log.h>

#include "aki/jsbind.h"

#include <fpdfview.h>
#include <fpdf_formfill.h>
#include <stdio.h>
#include <limits.h>
#include <multimedia/image_framework/image_mdk_common.h>
#include <multimedia/image_framework/image_pixel_map_mdk.h>

// ArkTSTutorial.pdf
//static napi_value Add(napi_env env, napi_callback_info info)
//{
//    //FPDF_FORMFILLINFO form_callbacks = {0};
//    
//    size_t argc = 2;
//    napi_value args[2] = {nullptr};
//
//    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
//
//    napi_valuetype valuetype0;
//    napi_typeof(env, args[0], &valuetype0);
//
//    napi_valuetype valuetype1;
//    napi_typeof(env, args[1], &valuetype1);
//
//    double value0;
//    napi_get_value_double(env, args[0], &value0);
//
//    double value1;
//    napi_get_value_double(env, args[1], &value1);
//
//    napi_value sum;
//    napi_create_double(env, value0 + value1, &sum);
//
//    OH_LOG_Print(LOG_APP, LOG_ERROR, 0xD0015B0, "pdfium", "pdfiumtest");
//    
//    return sum;
//
//}
//
//static napi_value OpenPdf(napi_env env, napi_callback_info info)
//{
//    size_t argc = 2;
//    napi_value args[2] = {nullptr};
//
//    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);
//    
//    char input_filename[PATH_MAX] = {0};
//    size_t length = 0;
//    napi_get_value_string_utf8(env, args[0], input_filename, PATH_MAX, &length);
//    
//    char outFile[PATH_MAX];
//    size_t outFileLength = 0;
//    napi_get_value_string_utf8(env, args[1], outFile, PATH_MAX, &outFileLength);
//    
//    
////    std::string  url = std::string((char *)buff, length);
////    LOGD("url: %{public}s", url.c_str());
//    OH_LOG_Print(LOG_APP, LOG_ERROR, 0xD0015B0, "pdfium", "file path = %{public}s out = %{public}s", input_filename, outFile);
//      FPDF_InitLibrary();
//    
//      FPDF_DOCUMENT doc = FPDF_LoadDocument(input_filename, NULL);
//    
//    FPDF_PAGE page = FPDF_LoadPage(doc, 0);
//      int width = (int)FPDF_GetPageWidth(page);
//      int height = (int)FPDF_GetPageHeight(page);
//    
//    OH_LOG_Print(LOG_APP, LOG_ERROR, 0xD0015B0, "pdfium", "width = %{public}d height = %{public}d", width, height);
//    
//    
//    int new_width = (int) (width * 300 / 72.0f); 
//    int new_height = (int) (height * 300 / 72.0f);
//    
//    OH_LOG_Print(LOG_APP, LOG_ERROR, 0xD0015B0, "pdfium", "new width = %{public}d height = %{public}d", new_width, new_height);
//    FPDF_BITMAP bitmap = FPDFBitmap_Create(new_width, new_height, 0);
//    FPDFBitmap_FillRect(bitmap, 0, 0, new_width, new_height, 0xFFFFFF);
//    FPDF_RenderPageBitmap(bitmap, page, 0, 0, new_width, new_height, 0, FPDF_ANNOT);
//    //FPDF_RenderPageBitmap(bitmap, page, 0, 0, new_width, new_height, 0, FPDF_LCD_TEXT | FPDF_RENDER_FORCEHALFTONE);
//    // 
//      const char *pixels = (char *)FPDFBitmap_GetBuffer(bitmap);
//
//  FILE *fp = fopen(outFile, "wb");
//  fprintf(fp, "P6\n%d %d\n255\n", new_width, new_height);
//  for (int i = 0; i < new_width * new_height; ++i) {
//    putc(pixels[4 * i + 2], fp);
//    putc(pixels[4 * i + 1], fp);
//    putc(pixels[4 * i + 0], fp);
//  }
//  fclose(fp);
//    
//    return nullptr;
//
//}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
//    napi_property_descriptor desc[] = {
//        { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
//        { "openPdf", nullptr, OpenPdf, nullptr, nullptr, nullptr, napi_default, nullptr },
//        
//    };
//    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    aki::JSBind::BindSymbols(env, exports);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
    napi_module_register(&demoModule);
}


